    #include <gtk/gtk.h>
gint8 *serializeTextBuffer(GtkTextBuffer *content_buffer, gsize *size);
gboolean deserialize2TextBuffer(GtkTextBuffer *dest_buffer, gint8 *data, gsize size);
GtkWidget *text_view, *text_view2;
void on_font_select_ok(GtkWidget *button, GtkFontSelectionDialog *data)
{
	
	
	gchar *s = gtk_font_selection_dialog_get_font_name(data);
/*

PangoFontDescription * pango_font_description_from_string
                                                        (const char *str);

void gtk_widget_modify_font (GtkWidget *widget, PangoFontDescription *font_desc);
*/
	PangoFontDescription *font_desc = pango_font_description_from_string(s);
	//g_free(s);
	g_printf("font=%s\n",s);
	gtk_widget_modify_font(text_view2, font_desc);
	gtk_widget_destroy(GTK_WIDGET(data));
}

void on_button_clicked(GtkWidget *button, gpointer userdata)
{
	GtkWidget *dialog;
	
	dialog = gtk_font_selection_dialog_new("请选择一种字体：");
	
	gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);

	gtk_signal_connect(GTK_OBJECT(dialog), "destroy", \
			GTK_SIGNAL_FUNC(gtk_widget_destroy), &dialog);

	gtk_signal_connect(GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(dialog)->ok_button), \
			"clicked", GTK_SIGNAL_FUNC(on_font_select_ok), \
			GTK_FONT_SELECTION_DIALOG(dialog));

	gtk_signal_connect(GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(dialog)->cancel_button), \
			"clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), \
			&dialog);
	gtk_widget_show(dialog);
}

int main(int argc, char *argv[])
{
	GtkWidget *window, *scwin;
	
	GtkWidget *vpaned, *vbox, *hbox;
	GtkWidget *label, *btn_font, *btn_color;

	GtkTextBuffer *buffer;
	GtkTextIter start, end;
	
	gtk_init(&argc, &argv);
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
	gtk_container_set_border_width(GTK_CONTAINER(window), 10);
	gtk_widget_set_size_request(window, 270, 350);
	gtk_window_set_title(GTK_WINDOW(window), "TextView");
 
//	vpaned = gtk_vbox_new(FALSE, 0);
	vpaned = gtk_vpaned_new();
	gtk_container_add(GTK_CONTAINER(window), vpaned);
	scwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	//gtk_box_pack_start(GTK_BOX(vbox), scwin, FALSE, FALSE, 0);
	gtk_paned_add1(GTK_PANED(vpaned), scwin);
	text_view = gtk_text_view_new();
	gtk_container_add(GTK_CONTAINER(scwin), text_view);

	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
	// 插入文字
	gtk_text_buffer_get_end_iter(buffer, &end);
	gtk_text_buffer_create_tag(buffer, "fg_blue", "foreground", "red", NULL);
	//gtk_text_buffer_insert(buffer, &end, "你好\n", -1);
	gtk_text_buffer_insert_with_tags_by_name(buffer, &end, "你好qq\n", -1, "fg_blue", NULL);
	
	gtk_text_buffer_get_start_iter(buffer, &start);
	gchar *text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
	g_printf("content:%s \n", text);
	
	// 第二个输入框
	vbox = gtk_vbox_new(FALSE, 0);
	gtk_paned_add2(GTK_PANED(vpaned), vbox);
	scwin = gtk_scrolled_window_new(NULL, NULL);
	//gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_box_pack_start(GTK_BOX(vbox), scwin, FALSE, FALSE, 0);	
	text_view2 = gtk_text_view_new();
	gtk_widget_set_size_request(text_view2, 200, 100);
	gtk_container_add(GTK_CONTAINER(scwin), text_view2);
	btn_font = gtk_button_new_with_label("选择字体");
	gtk_box_pack_start(GTK_BOX(vbox), btn_font, FALSE, FALSE, 0);
	btn_color = gtk_button_new_with_label("选择颜色");
	gtk_box_pack_start(GTK_BOX(vbox), btn_color, FALSE, FALSE, 0);
	
	g_signal_connect(G_OBJECT(btn_font), "clicked", G_CALLBACK(on_button_clicked), text_view2);

	GtkTextBuffer *buffer2 = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view2));
	PangoFontDescription *font_desc = pango_font_description_from_string("Sans Bold Italic 32");
	gtk_widget_modify_font(text_view2, font_desc);

//	gtk_text_buffer_create_tag(buffer2, "fg_blue", "foreground", "blue", NULL);
	GtkTextIter end2;	
	gtk_paned_set_position(GTK_PANED(vpaned), 160);
	gtk_text_buffer_get_end_iter(buffer2, &end2);
	gtk_text_buffer_insert(buffer2, &end2, "helloqqqqqqqq", -1);
//	gtk_text_buffer_insert_with_tags_by_name(buffer2, &end2, "helloword!!\n", -1, "fg_blue", NULL);
	
//	将内容序列化	
	gsize size = 0;
	GtkTextBuffer *content_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view2));
	gint8 *serializedData = serializeTextBuffer(content_buffer, &size);
	// 反序列化	
	deserialize2TextBuffer(buffer, serializedData, size);
	

	g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
	gtk_widget_show_all(window);
	gtk_main();
	return 0;
}

/*
 *
 *
 *
 */
gint8 *serializeTextBuffer(GtkTextBuffer *content_buffer, gsize *size)
{
	// 注册序列化格式
	GtkTextBuffer *register_buffer = gtk_text_buffer_new(NULL);
	GdkAtom  format = gtk_text_buffer_register_serialize_tagset(register_buffer, NULL);
	// 序列化	
	*size = 0;
	GtkTextIter start_iter, end_iter;
	gtk_text_buffer_get_start_iter(content_buffer, &start_iter);
	gtk_text_buffer_get_end_iter(content_buffer, &end_iter);
	guint8 *serializedData = gtk_text_buffer_serialize (register_buffer, content_buffer, \
                                                         format, &start_iter, &end_iter, \
                                                         size);

	return serializedData;
}

/*
 *
 *
 *
 */
gboolean deserialize2TextBuffer(GtkTextBuffer *dest_buffer, gint8 *data, gsize size)
{
	GError *error = NULL;
	GtkTextIter dest_iter;
	gtk_text_buffer_get_end_iter(dest_buffer, &dest_iter);
	GtkTextBuffer *register_buffer = gtk_text_buffer_new(NULL);
	GdkAtom  format = gtk_text_buffer_register_deserialize_tagset(register_buffer, NULL);
	gboolean flag = gtk_text_buffer_deserialize(register_buffer, dest_buffer, format, &dest_iter, \
                                   data, size, &error);

	if(error != NULL){
		g_printf("&&%d %s\n",flag, error->message);
	}
	return flag;
}
/******序列化相关函数******************************************
GdkAtom		gtk_text_buffer_register_serialize_tagset (GtkTextBuffer *buffer,
                                        		 const gchar *tagset_name);

guint8		*gtk_text_buffer_serialize (GtkTextBuffer *register_buffer,
                                         GtkTextBuffer *content_buffer,
                                         GdkAtom format,
                                         const GtkTextIter *start,
                                         const GtkTextIter *end,
                                         gsize *length);

GdkAtom		gtk_text_buffer_register_deserialize_tagset
                                        (GtkTextBuffer *buffer,
                                         const gchar *tagset_name);

gboolean	gtk_text_buffer_deserialize (GtkTextBuffer *register_buffer,
                                         GtkTextBuffer *content_buffer,
                                         GdkAtom format,
                                         GtkTextIter *iter,
                                         const guint8 *data,
                                         gsize length,
                                         GError **error);
******************************************************	
	*/
